package com.yunkuhui.www.demo.configuration;

import com.yunkuhui.www.demo.interceptor.LogInterceptor;
import com.yunkuhui.www.demo.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class InterceptorConfiguration {

	@Resource
	private LogInterceptor logInterceptor;

	@Resource
	private MyInterceptor myInterceptor;

	@Bean
	public WebMvcConfigurer webMvcConfigurer() {
		return new WebMvcConfigurer() {

			@Override
			public void addInterceptors(InterceptorRegistry registry) {
				List<String> list = new ArrayList<>();
				// 拦截器拦截的 uri 从 controller 的配置算起，即在此项目不包括demo
				// 即，不能是/demo/interceptor/1
				list.add("/interceptor/1");
				list.add("/interceptor/2");
				registry.addInterceptor(logInterceptor).addPathPatterns(list);
				list.clear();
				list.add("/**");
				registry.addInterceptor(myInterceptor).addPathPatterns(list);
			}

			@Override
			public void addViewControllers(ViewControllerRegistry registry) {
				// 无controller，注册路由
				registry.addViewController("/demo/interceptor/0");
			}
		};
	}
}
